Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sympy parsing workaround for specific tokens #5178

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

mwdchang
Copy link
Member

@mwdchang mwdchang commented Oct 17, 2024

Summary

When rate-laws contain specific tokens sympy parsing tend to break as they are a part of reserved symbols. To parse them properly we encode them to something else, then revert them back when we need the parsing result.

So far, we have

  • lambda
  • S
  • Ci

For example:

>>> parse_expr("A")
A
>>> parse_expr("A + 1")
A + 1
>>> parse_expr("S + 1")
ValueError: Error from parse_expr with transformed code: 'S +Integer (1 )'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/dchang/venv/3.11/lib/python3.11/site-packages/sympy/parsing/sympy_parser.py", line 1087, in parse_expr
    raise e from ValueError(f"Error from parse_expr with transformed code: {code!r}")
  File "/Users/dchang/venv/3.11/lib/python3.11/site-packages/sympy/parsing/sympy_parser.py", line 1078, in parse_expr
    rv = eval_expr(code, local_dict, global_dict)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dchang/venv/3.11/lib/python3.11/site-packages/sympy/parsing/sympy_parser.py", line 906, in eval_expr
    expr = eval(
           ^^^^^
  File "<string>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'SingletonRegistry' and 'One'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant